# 1.配置
# 'django.contrib.auth',
# 'django.contrib.contenttypes',
# 中间件
# SessionMiddleware
# 'django.contrib.auth.middleware.AuthenticationMiddleware',
#
# Django内置user模型


# 核心User
# from django.contrib.auth.models import User

# from django.shortcuts import render
# from django.http import HttpResponse
# from django.contrib.auth.models import User
#
#
# def index(request):
#     user = User.objects.create_user(username="mingda", email="mingda@qq.com", password="11111111")
#     创建超级用户
#     super_user = User.objects.create_superuser(username="admin", email="admin@qq.com", password="admin111")
#     return HttpResponse('hello')
# 也可以
# python manage.py createsuperuser


# 修改密码
# user = User.objects.get(pk=1)
# user.set_password("22222222")


# 验证
# from django.contrib.auth import authenticate
# username = "mingda"
# password = "22222222"
# user = authenticate(request, username=username, password=password)
# if user:
#     print("success")
# else:
#     print("wrong")


# 1. 当内置user不满足我们的要求,可以对user进行扩展
# from django.db import models
# from django.contrib.auth.models import User
#
#
# class Person(User):
#     class Meta:
#         # 代理模型，操作Person，实际上就是操作User，不能在代理模型添加新的field
#         # 为了增加更多操作
#         proxy = True
#
#     @classmethod
#     def get_back(cls):
#         return cls.objects.first(is_active=False)
#     # 在其他文件中可以直接利用Person.get_back调用，因为原来的User不能直接获取黑名单

# 2.可以使用一对一关系表添加新字段
from django.dispatch import receiver
from django.db.models.signals import post_save
# class UserExtension(models.Model):
#     user = models.OneToOneField(User, on_delete=models.CASCADE)
#     school = models.CharField(max_length=100)
#     phone = models.CharField(max_length=11)
#
#
# @receiver(post_save, sender=User)
# def handle_user_extension(sender, instance, created, **kwargs):
#     if created:
#         UserExtension.objects.create(user=instance)
#     else:
#         instance.extension.save()

# 3.使用继承
# 继承在做数据库迁移时，必须是第一次映射
# class User(AbstractUser):
#     phone = models.CharField(max_length=11, unique=True)
#     school = models.CharField(max_length=100)
#
#     # 这个from django.contrib.auth import authenticate函数验证的不再是username
#     USERNAME_FIELD = 'phone'
# 配置setting文件
# AUTH_USER_MODEL = 'front.User'

# class UserManager(BaseUserManager):
#     """
#     实现User的 object功能
#     """
#     def _create_user(self, telephone, username, password, **kwargs):
#         user = self.model(telephone=telephone, username=username, **kwargs)
#         user.set_password(password)
#         user.save()
#         return user
#
#     def create_user(self, telephone, username, password, **kwargs):
#         kwargs['is_superuser'] = False
#         return self._create_user(telephone, username, password, **kwargs)
#
#     def create_superuser(self, telephone, username, password, **kwargs):
#         kwargs['is_superuser'] = True
#         return self._create_user(telephone, username, password, **kwargs)
#
#
# # 继承在做数据库迁移时，必须是第一次映射
# class User(AbstractUser):
#     phone = models.CharField(max_length=11, unique=True)
#     school = models.CharField(max_length=100)
#
#     # 这个from django.contrib.auth import authenticate函数验证的不再是username,但还是叫username，只是带表的是phone
#     USERNAME_FIELD = 'phone'

# 4.使用继承AbstractBaseUser
# class User(AbstractBaseUser, PermissionManager):
#     email = models.CharField(max_length=110, unique=True)
#     phone = models.CharField(max_length=11, unique=True)
#     school = models.CharField(max_length=100)
#     username = models.CharField(max_length=100)
#     is_active = models.BooleanField(default=True)
#
#     USERNAME_FIELD = 'phone'
#     REQUIRED_FIELDS = []
#
#     objects = UserManager()


from django.contrib.auth import login
from django.contrib.auth.decorators import login_required,permission_required
# @login_required(login_url="")
# permission_required（"app名.权限", login_url, raise_exception）看有没有权限，首先看有没有登录，然后再看有没有权限

